
import traceback
from views.base import BaseHandler
from db import db
from util.myredis import r
from elasticsearch import Elasticsearch
es = Elasticsearch("http://101.43.203.36:9200/")
class Findes(BaseHandler):
    def get(self):
        # try:
        sql = "select * from landmarks1 where status=1 union all select * from landmarks2 where status=1 union all select * from landmarks where status=1"
        res = db.findall(sql)
        print(res)
        if res:
            for i in res:
                es.index(index="landmarks2", doc_type="KK",body={
                    # 'table_name':'landmarks',
                    'code':i['code'],
                    'descrip':i['descrip'],
                    'purpose':i['purpose'],
                    'tmoney':i['tmoney'],
                    'pay_type':i['pay_type'],
                    'interest_rate':i['interest_rate'],
                    'term':i['term'],
                    'userid':i['userid'],
                    'status':i['status'],
                    'audit_userid':i['audit_userid'],
                    'audit_time':i['audit_time'],
                    'money':i['money'],
                    'due_time':i['due_time']
                })
                # print(i['term'])
                print("###")
        # except:
        #     print("&&&")
            # error = traceback.format_exc()
            # print("Error: unable to fecth data", error)
            return 'ok'
        print("cuo")
        return '错误'

def updatedata():
    key = 'eslastdata'
    v = r.str_get(key)
    if v:
        sql = "select * from landmarks1 where status=1 union all select * from landmarks2 where status=1 union all select * from landmarks where status=1 where code>order by code asc"
    else:
        sql = "select * from landmarks1 where status=1 union all select * from landmarks2 where status=1 union all select * from landmarks where status=1 order by code asc"
    res = db.update(sql)
    db.commit()
    r.set(key,res[-1]['code'])

class Getlandmarkses(BaseHandler):
    def get(self):
        page = self.get_query_argument('page')
        purpose = self.get_query_argument('biao')
        print(purpose)
        pagesize= 2
        start = (int(page)-1)*pagesize
        dsl = {
            "query":{
                "match":{'purpose':purpose}
            },
            "from":start,
            "size":pagesize,
            "sort":{
                "code":{
                    'order':'desc'
                }
            }
        }
        dss = {
            "query":{
                "match":{'purpose':purpose}
            }
        }
        res = es.search(index='landmarks2',body=dsl)
        count = es.count(index='landmarks2',body=dss)
        cc= count['count']
        # print('*************',res['hits'])
        print(res['hits']['hits'])

        list = [i['_source']  for i in (res['hits']['hits'])]

        print(list)
        return self.finish({'code':200,'list':list,'count':cc})


class LandSearch(BaseHandler):
    def get(self):
        page = self.get_query_argument('page')
        page_size = 2
        descrip = '"' + self.get_query_argument('descrip') + '"'
        print(type(descrip))
        purpose = self.get_query_argument('purpose')
        start = (int(page) - 1) * int(page_size)
        stra = '"match_all":{}'

        flag = False

        ss = '"bool":{"must":[{"match":{"'

        if descrip:
            flag = True
            ss = ss + 'descrip":'+ descrip + '}}'

        if purpose:
            if flag == True:
                ss = ss + ',{"match":{"purpose":' + purpose + '}}'
            else:
                flag = True
                ss = ss + 'purpose":' + purpose + '}}'

        if flag == True:
            stra = ss + ']}'

        dsl = '{' + '"query":{' + stra + '},"from":0,"size":20}'

        print(dsl)
        match_data = es.search(index="land", body=dsl)
        results = []
        for hit in match_data['hits']['hits']:
            results.append(hit['_source'])
        return self.finish({'code':200,'data':results})






